<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\model;

use think\Model;
use app\shop\model\ShopMember;

class Member extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__MEMBER__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    public static function exportSelect($where=[],$order=''){
        $data_list = self::view('member', true)
            ->view('member_level','name levelname','member_level.id=member.member_level','left')
            ->view('member_industry','name indname','member_industry.id=member.industry','left')
            ->view('company_nature','company_nature cn','company_nature.id=member.company_nature','left')
            ->view('member_department','department_id,department dname','department_id=member.department','left')
            ->where($where)
            ->order($order)
            ->paginate();

        foreach ($data_list as $key=>$value){

            if($value['sex']==0){
                $data_list[$key]['sex']='男';
            }else{
                $data_list[$key]['sex']='女';
            }
            if($value['status']==0){
                $data_list[$key]['status']='待审核';
            }else{
                $data_list[$key]['status']='已审核';
            }
            if($value['is_enable']==1){
                $data_list[$key]['is_enable']='启用';
            }else{
                $data_list[$key]['is_enable']='禁用';
            }
        }
        return $data_list;
    }
    /**
     * 对明文密码，进行加密，返回加密后的密码
     * @param $identifier 为数字时，表示uid，其他为用户名
     * @param type $pass 明文密码，不能为空
     * @return type 返回加密后的密码
     */
    public static function encryption($pass, $verify = "") {
        $pass = md5($pass . md5($verify));
        return $pass;
    }
    /**
     * 根据标识修改对应用户密码
     * @param type $identifier
     * @param type $password
     * @return type
     */
    public static function ChangePassword($identifier, $password) {
        if (empty($identifier) || empty($password)) {
            return false;
        }
        $term = array();
        if (is_int($identifier)) {
            $term['id'] = $identifier;
        } else {
            $term['user_name'] = $identifier;
        }
        $verify =  genRandomString(6);
        $data['verify'] = $verify;
        $data['password'] = self::encryption($password, $verify);
        $up = self::where($term)->update($data);
        if ($up !== false) {
            return true;
        }
        return false;
    }

    /**
     * 根据提示符(username)和未加密的密码(密码为空时不参与验证)获取本地用户信息
     * @param type $identifier 为数字时，表示uid，其他为用户名
     * @param type $password
     * @return 成功返回用户信息array()，否则返回布尔值false
     */
    public static function getLocalAdminUser($identifier, $password = null) {
        if (empty($identifier)) {
            return false;
        }
        $map = array();
        if (is_int($identifier)) {
            $map['id'] = $identifier;
        } else {
            $map['user_name|mobile|l_mobile'] = $identifier;
        }
        $user = self::where($map)->find();
        if (!$user) {
            return false;
        }
        if ($password) {
            //验证本地密码是否正确
            if (self::encryption($password, $user['verify']) != $user['password']) {
                return false;
            }
        }
        return $user;
    }

    /**
     * 记录前台登陆信息
     * @param string $identifier 用户名
     * @param string $password 用户密码
     * @param int $status 状态 1登录成功 0登录失败
     * @param string $info 备注
     * @author yiyouguisu<741459065@qq.com> time|20161111
     */
    public static function recordLoginHome($identifier, $password, $status, $info = "") {
        db("userlog")->insert([
            "username" => $identifier,
            "logintime" => date("Y-m-d H:i:s"),
            "loginip" => get_client_ip(),
            "status" => $status,
            "password" => "***" . substr($password, 3, 4) . "***",
            "info" => $info
        ]);
    }

    /**
     * 获取会员等级，如果第三方店铺设置了会员等级，则以第三方店铺设置为准，如果没有设置，用平台的会员等级
     * @author sky
     */
    public function getMemberLevel($mid,$shopId)
    {
        //第三方店铺会员等级
        $ShopMemberModel = new ShopMember();
        $shopMemberInfo = $ShopMemberModel->getShopMemberInfo($shopId,$mid);
        if($shopMemberInfo){
            $memberLevel = $shopMemberInfo['member_level'];
        }else{
            $memberLevel = self::get($mid)->member_level;
        }
        return $memberLevel;
    }
    
}